<template>
  <el-main>
    <div class="margin-bottom-20">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/user/staff' }"
          >用户管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>员工列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="clearfix">
      <el-form class="pull-left" inline>
        <el-form-item>
          <el-input
            placeholder="请输入账号"
            v-model="search_param.accountName"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-input
            placeholder="请输入手机号"
            v-model="search_param.mobile"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            placeholder="请选择人员类别"
            v-model="search_param.authGroupId"
          >
            <el-option
              :key="item.id"
              :label="item.groupName"
              :value="item.id"
              v-for="item in authGroupList"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            placeholder="请选择隶属公司"
            v-model="search_param.companyId"
          >
            <el-option
              :key="item.id"
              :label="item.companyName"
              :value="item.id"
              v-for="item in companyList"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button
              @click="handleSearch"
              icon="el-icon-search"
              size="medium"
              type="primary"
              >搜索
            </el-button>
            <el-button @click="handleReset" size="medium" type="primary"
              >重置
            </el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
      <el-form class="pull-right" inline>
        <el-form-item>
          <el-button-group>
            <el-button @click="handleAdd" size="medium" type="primary"
              >新增员工
            </el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
    </div>
    <el-card class="box-card" shadow="never">
      <el-table :data="table_data" stripe style="width: 100%">
        <el-table-column
          label="账号"
          prop="accountName"
          show-overflow-tooltip
          width="120"
        />
        <el-table-column
          label="员工姓名"
          prop="realName"
          show-overflow-tooltip
          width="120"
        />
        <el-table-column label="手机号" prop="mobile" show-overflow-tooltip />
        <el-table-column
          label="人员类别"
          prop="authGroupName"
          show-overflow-tooltip
        />
        <el-table-column
          label="工作区域"
          prop="address"
          show-overflow-tooltip
        />
        <el-table-column label="注册时间" prop="ctime" width="160" />
        <el-table-column
          label="隶属公司"
          prop="companyName"
          show-overflow-tooltip
        />
        <el-table-column label="简介" prop="remark" show-overflow-tooltip />
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row)" size="mini" type="text"
              >修改
            </el-button>
            <el-button @click="handleBind(scope.row)" size="mini" type="text"
              >管理设备
            </el-button>
            <el-button @click="handleDelete(scope.row)" size="mini" type="text"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="margin-top-10 clearfix">
        <el-pagination
          :current-page.sync="current_page"
          :page-size.sync="page_size"
          :page-sizes="[5, 10, 20]"
          :total="total_size"
          @current-change="load_table_data"
          @size-change="load_table_data"
          background
          class="pull-right"
          layout="total, sizes,  prev, pager, next"
        />
      </div>
    </el-card>
  </el-main>
</template>

<script>
import { mapActions, mapState } from "vuex";

export default {
  data() {
    return {
      companyList: [],
      authGroupList: []
    };
  },
  mounted() {
    this.load_table_data();
    this.companyOptions();
    this.authGroupOptions();
  },
  computed: {
    ...mapState("staff", ["total_size", "table_data"]),
    page_size: {
      get() {
        return this.$store.state.staff.page_size;
      },
      set(value) {
        this.set_page_size(value);
      }
    },
    current_page: {
      get() {
        return this.$store.state.staff.current_page;
      },
      set(value) {
        this.set_current_page(value);
      }
    },
    search_param: {
      get() {
        return this.$store.state.staff.search_param;
      }
    }
  },
  methods: {
    ...mapActions("staff", [
      "load_table_data",
      "set_page_size",
      "set_current_page",
      "set_search_param"
    ]),
    handleSearch() {
      this.set_search_param(Object.assign({}, this.search_param));
      this.load_table_data();
    },
    handleReset() {
      this.set_search_param({});
      this.load_table_data();
    },
    handleAdd() {
      this.$router.push("/user/staff/add");
    },
    handleEdit(row) {
      this.$router.push("/user/staff/edit/" + row.id);
    },
    handleBind(row) {
      this.$router.push("/user/staff/bind/" + row.id);
    },
    handleDelete(row) {
      this.$confirm(
        "此操作将永久删除员工：" + row.accountName + " , 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        let loadingInstance = this.$loading({ fullscreen: true });

        this.$ajax.staff
          .remove(row.id)
          .then(result => {
            if (result.code != 1) {
              this.$message({
                message: result.msg,
                type: "warning"
              });
            } else {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.load_table_data();
            }
          })
          .catch(e => {
            console.log(e);
            this.$message({
              message: "系统繁忙，请稍后重试",
              type: "warning"
            });
          })
          .finally(function() {
            loadingInstance.close();
          });
      });
    },
    companyOptions() {
      this.$ajax.company
        .options()
        .then(result => {
          if (result.code == 1) {
            this.companyList = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    },
    authGroupOptions() {
      this.$ajax.auth_group
        .options()
        .then(result => {
          if (result.code == 1) {
            this.authGroupList = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    }
  }
};
</script>
